﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomahawk.Runtime.Logic.UI.Components;
using System.Xml.Serialization;

namespace Tomahawk.Runtime.Logic.UI
{
    public class XUILabel : XUIPlaceableControl
    {
        #region "Serializable properties"

        [XmlElement("Image")]
        public ImageComponent image;

        [XmlElement("Text")]
        public TextComponent text;

        #endregion

        /// <summary>
        /// Control Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            // image components
            this.image.Initialize();

            // text component
            this.text.Initialize();

            //copy position and size to sprites
            this.passPositionToSprites();

            //not acepting focus
            this.CanHaveFocus = false;
        }

        /// <summary>
        /// Controls visibility changes
        /// </summary>
        protected override void OnChangeVisibility(bool newValue)
        {
            base.OnChangeVisibility(newValue);
            this.image.SetVisibility(false);
            this.text.SetVisibility(false);
        }

        #region "Positioning"

        /// <summary>
        /// Handle a position change
        /// </summary>
        protected override void OnPositionChange()
        {
            base.OnPositionChange();
            this.passPositionToSprites();
        }

        /// <summary>
        /// Copy the control position to the sprites
        /// </summary>
        private void passPositionToSprites()
        {
            this.image.SetPosition(ref this.renderRect);
            this.text.SetPosition(ref this.renderRect);
        }

        #endregion

        /// <summary>
        /// Destruction handling
        /// </summary>
        public override void OnBeforeDestroy()
        {
            base.OnBeforeDestroy();
            this.image.Destroy();
            this.text.Destroy();
        }

        #region "Readyness"

        /// <summary>
        /// Returns true when all the resource dependant objects are ready
        /// </summary>
        protected override bool checkResourcesReady()
        {
            return base.checkResourcesReady() &&
                this.image.IsReady() &&
                this.text.IsReady();
        }

        #endregion
    }

}
